import { baseApiUrl, path } from "../../utils/config";
import Toast from '@vant/weapp/toast/toast';
import { userInfo, carmiExchange, pandaReceive, videoOpen } from '../../utils/api';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,
    head_img: "",
    nickname: "",
    id: "",
    integral: "",
    panda: 0,
    pile_owner: null,

    card_no: "",
    exchange_show: false,
    xm_show: false,
    list: [
      // { name: "已购买", url: "/pages/my_course/my_course", src: `${baseApiUrl}/images/m1.png` },
      { name: "地址管理", url: "/pages/address_list/address_list", src: `${baseApiUrl}/images/m2.png` },
      // { name: "个人风采", url: "", src: `${baseApiUrl}/images/m3.png` },
      { name: "我的订单", url: "/pages/order_list/order_list", src: `${baseApiUrl}/images/m4.png` },
      { name: "邀请好友", url: "/pages/invite/invite", src: `${baseApiUrl}/images/m5.png` },
      { name: "联系我们", url: "", src: '../../images/bc1.png', open: 1 },
      { name: "用户协议", url: "/pages/setting/setting?key=agreement", src: `${baseApiUrl}/images/m6.png` },
      { name: "关于我们", url: "/pages/setting/setting?key=about", src: `${baseApiUrl}/images/m7.png` },
    ],
    contact_show: false,
    customer: ""
  },
  async onShow() {
    let res = await userInfo();
    let pile_owner = res.pile_owner;
    let customer = res.customer;
    let { head_img, nickname, id, integral, panda } = res.user;
    this.setData({ head_img, nickname, id, integral, panda, pile_owner, customer });

    let { is_open } = await videoOpen();
    if (is_open != 1) return;
    const list = this.data.list;
    list.unshift({ name: "已购买", url: "/pages/my_course/my_course", src: `${baseApiUrl}/images/m1.png` });
    this.setData({ list });
  },
  changePopup(e) {
    let { key } = e.currentTarget.dataset;
    this.setData({ [key]: !this.data[key], card_no: '' });
  },
  async submitExchange() {
    let { card_no } = this.data;
    await carmiExchange({ card_no })
    Toast.success('兑换成功');
    setTimeout(() => {
      this.setData({ card_no: '', exchange_show: false });
    }, 1500);
  },
  async submitExchangePanda() {
    await pandaReceive();
    Toast.success('兑换成功');
    setTimeout(() => {
      this.setData({ xm_show: false });
    }, 1500);
  },
  downloadFile() {
    let customer = this.data.customer;
    wx.downloadFile({
      url: customer,
      success: (res) => {
        let filePath = res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath,
          success: (res) => Toast.success('保存成功')
        })
      }
    })
  },
  submitCode() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.writePhotosAlbum']) {
          this.downloadFile();
          return false;
        }
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => this.downloadFile(),
            fail: (err) => {
              wx.showModal({
                title: '授权保存',
                content: '拒绝授权,将无法正常保存图片',
                confirmText: "授权",
                success: (res) => {
                  if (res.confirm) {
                    wx.openSetting({
                      success: (res) => Toast.success('授权成功')
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  request(e) {
    let { pile_owner } = this.data;
    // if (pile_owner) return;
    let { url } = e.currentTarget.dataset;
    wx.navigateTo({ url });
  },
  router(e) {
    let { url, open } = e.currentTarget.dataset;
    if (url) {
      wx.navigateTo({ url });
      return false;
    }
    if (open == 1) {
      this.setData({
        contact_show: true
      })
    }
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})